<?php
/**
 * Created by RegionCar.php.
 * User: wy
 * Date: 2019/4/18
 * Time: 14:26
 */

namespace app\admin\controller;
use think\Db;
use app\admin\model\RegionModel;
class RegionCar extends Base
{
    public function index(){
        $page = input('get.page');
        $limit = input('get.limit');
        $map = [];
        if ($page && $limit) {
            if(input('get.region_name')){
                $region_name = input('get.region_name');
                $map['b.region_name'] =  ['like',"%$region_name%"];
            }
            $start = ($page - 1) * $limit;
            $count = Db::name('region_car a')
                ->join('region b','a.region_id=b.region_id','LEFT')
                ->where($map)->count();
            $list = Db::name('region_car a')
                ->field('a.id,a.name,a.sort,b.region_name,a.is_open,a.nid')
                ->join('region b','a.region_id=b.region_id','LEFT')
                ->where($map)->limit($start, $limit)->order('a.region_id asc,sort asc')->select();
            foreach ($list as $key => $value) {
                $list[$key]['is_open']  = $value['is_open']==1?'开启':'关闭';
            }
            return json([
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list
            ]);
        }
        return $this->fetch();
    }

    //新增城市车辆种类
    public function add(){
        if(input('post.')){
            $data = input('post.');
            $map['name'] = $data['name'];
            $map['region_id'] = $data['city_id'];
            $info = Db::name('region_car')->where($map)->find();
            if(!empty($info)){
                $this->error('对不起该名称已存在', '', '', 1);
            }
            $add['name'] = input('post.name');
            $add['region_id'] = input('post.city_id');
            $add['sort'] = input('post.sort');
            $add['is_open'] = input('post.is_open');
            $add['nid'] = input('post.nid');
            Db::name('region_car')->insertGetId($add);
            return "<script type='application/javascript'>parent.window.location.reload();</script>";
        }
        //查询省市区
        $region_model = new RegionModel();
        //查询获取省份
        $where = [
            'parent_id'     => 1,
            'region_level'  => 1
        ];
        $provinceList = $region_model->getRegion($where,'region_id,region_name');
        $this->assign('provinceList',$provinceList);
        return $this->fetch();
    }

    //修改
    public function edit(){
        if(input('post.')){
            if(input('post.id')){
                $data = input('post.');
                $map['name'] = $data['name'];
                $map['region_id'] = $data['region_id'];
                $map['id'] = ['<>',$data['id']];
                $info = Db::name('region_car')->where($map)->find();
                if(!empty($info)){
                    $this->error('对不起该名称已存在', '', '', 1);
                }
                $update['id'] = input('post.id');
                $update['name'] = input('post.name');
                $update['region_id'] = input('post.region_id');
                $update['sort'] = input('post.sort');
                $update['is_open'] = input('post.is_open');
                $update['nid'] = input('post.nid');
                $result = Db::name('region_car')->update($update);
                if($result){
                    return "<script type='application/javascript'>alert('修改成功');parent.window.location.reload();</script>";
                    //$this->success('修改成功', '/admin/CarBrand/Index', '', 1);
                }
            }
        }elseif(input('get.id')){
            $id = input('get.id');
            $data = Db::name('region_car a')
                ->field('a.id,a.name,a.sort,a.region_id,a.is_open,b.region_name,a.nid')
                ->join('region b','a.region_id=b.region_id','LEFT')
                ->where('id',$id)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $this->error('无效的ID', '', '', 1);
        }
    }

    //删除
    public function del(){
        if(input('get.id')){
            $id = input('get.id');
            $data = Db::name('region_car')->where('id',$id)->delete();
            if($data){
                exit(json_encode(['status'=>1,'msg'=>'删除成功']));
            }else{
                exit(json_encode(['status'=>0,'msg'=>'删除失败']));
            }
        }else{
            $this->error('无效的ID', '', '', 1);
        }
    }
}